package com.senjoeson.socialsecure.utils;

import com.senjoeson.socialsecure.model.ArticleListBean;
import com.senjoeson.socialsecure.model.ReComment;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class ParseUtils {


    public static void main(String[] args) {
        //  String url = "http://new.sysb.gov.cn/pages/home/more_1.html";
        //   getArticles3Tab(url);
        //  String url = "http://new.sysb.gov.cn/pages/2/more_2.html";
        //  String service_url = "http://new.sysb.gov.cn/pages/12/more_12.html";
        //  parseData44Tab(url);
        //  System.out.println("--------------------------------------------------");
        //  parseData44Tab(service_url);
        //   getHeadUrl(url);

        //  parseData44Tab("http://new.sysb.gov.cn/pages/download.html");
        // parseComment("731221");
      //  parseComment("731200");
    }

    /**
     * 三个tab的数据加载方法
     *
     * @param url 头部 http://new.sysb.gov.cn/
     * @return
     */
    public static List<ArticleListBean> getArticles3Tab(String url) {
        String headUrl = "http://new.sysb.gov.cn";
        ArrayList<ArticleListBean> articleListBeans = new ArrayList<>();
        try {
            Element parent = Jsoup.connect(url).get().body();
            Elements a = parent.select("a");
            Elements font = parent.select("font");
            for (int i = 0; i < a.size(); i++) {
                ArticleListBean articleListBean = new ArticleListBean();
                String href = a.get(i).attr("href");
                String title = a.get(i).text();
                String time = font.get(i).text();
                articleListBean.setArticleUrl(headUrl + href);
                articleListBean.setTitle(title);
                articleListBean.setTime(time);

                articleListBeans.add(articleListBean);
                //System.out.println(href);
                //System.out.println(title);
                //System.out.println(time);
            }
            return articleListBeans;

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 后面四个TAB的数据
     *
     * @param url 地址
     * @return
     */
    public static List<ArticleListBean> parseData44Tab(String url) {
        String headUrl = getHeadUrl(url);
        ArrayList<ArticleListBean> articleListBeans = new ArrayList<>();
        try {
            Element element = Jsoup.connect(url).get().body().getElementById("xinwen");
            //  System.out.println(element);
            Elements tr = element.select("tr");
            System.out.println(tr);
            for (int i = 0; i < tr.size(); i++) {
                ArticleListBean articleListBean = new ArticleListBean();
                Elements td = tr.get(i).select("td");
                String href = "";
                String title = "";
                String textFlag = "";
                String time = "";

                if (td.size() == 5) {
                    title = td.get(1).text();
                    href = td.get(4).select("a").attr("href");
                    time = td.get(3).text();
                    textFlag = td.get(2).text();
                }
                if (td.size() == 3) {
                    textFlag = td.get(1).text();
                    time = td.get(2).text();
                    href = td.get(0).select("a").attr("href");
                    title = td.get(0).text();
                } else if (td.size() == 2) {
                    time = td.get(1).text();
                    href = td.get(0).select("a").attr("href");
                    title = td.get(0).text();
                }


                // System.out.println(href);
                // System.out.println(title);
                // System.out.println(textFlag);
                // System.out.println(time);
                articleListBean.setTime(time);
                articleListBean.setTitle(title);

                if (td.size() == 5) {
                    articleListBean.setArticleUrl("http://new.sysb.gov.cn" + href);
                } else {
                    articleListBean.setArticleUrl(headUrl + href);
                }
                // if (!TextUtils.isEmpty(textFlag)) {
                articleListBean.setTextFlag(textFlag);
                //}
                if (articleListBeans.size() < 30) {
                    articleListBeans.add(articleListBean);
                }

            }
            return articleListBeans;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static String getHeadUrl(String url) {
        int i = url.lastIndexOf("/");
        String substring = url.substring(0, i + 1);
        System.out.println(substring);
        return substring;
    }


    public static ReComment parseComment(String uuid) {
        ReComment reComment = new ReComment();
        String url = "http://new.sysb.gov.cn/mess/detail" + uuid;
        try {
            Element body = Jsoup.connect(url).get().body();
            System.out.println(body);
            String name = body.getElementById("twr").text();
            String time = body.getElementById("twsj").text();
            String title = body.getElementById("twbt").text();
            String content = body.getElementById("twnr").text();

            reComment.setContent(content);
            reComment.setName(name);
            reComment.setTime(time);
            reComment.setTitle(title);

            Element tbody = body.getElementById("list");
            Elements select = tbody.select("td").select("p");
         //   String centerContent = select.get(0).text();
            String leftContent = select.get(1).text();
            reComment.setRecomment(leftContent);
            System.out.println(select);
            return reComment;

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


}
